﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twitter.Tests;
using NUnit.Framework;
using Twitter;
using FriendsNearby.Services;
using Moq;
using FriendsNearby.Services.Entities;
using InSTEDD.Labs.Geo;
using System.Linq.Expressions;

namespace FriendsNearbyBot.Tests
{
    [TestFixture]
    public class PingProcessorFixture : TestBase
    {
		Mock<ILocationService> locationService;
		Mock<IUsersService> usersService;
		Guid me = Guid.Empty;

		[SetUp]
		public void CreateServices()
		{
			locationService = new Mock<ILocationService>();
			usersService = new Mock<IUsersService>();

			usersService.Expect(x => x.QueryUserRegistry(It.IsAny<Expression<Func<UserRegistry, bool>>>())).Returns(new UserRegistry { UserID = me });
		}

        [Test]
        public void ShouldSendMeessageIfLastUpdateIsNotKnown()
        {
            locationService.Expect(x => x.GetLastLocationInfo(me)).Returns((LocationInfo)null);

            MemoryBotClient bot = new MemoryBotClient("fnb");
            MemoryBotClient user = new MemoryBotClient("fnbtesta");
            bot.SetBuddy(user);
            user.SetBuddy(bot);

			PingProcessor processor = new PingProcessor(usersService.Object, locationService.Object, CreateMessageBuilder());
            Message message = CreateMessage("ping", "fnbtesta", "fnb");

            Assert.IsTrue(processor.CanProcess(message));
            var runNext = processor.Process(bot, message);
            Assert.AreEqual(RunNextProcessor.No, runNext);

            string text = "Service is running. There is no previous location. This message was sent at";

            Assert.AreEqual(1, user.Messages.GetAll().Count());
            AssertString.StartsWith(text, user.Messages.GetAll().First().Text);
            AssertString.EndsWith("UTC.", user.Messages.GetAll().First().Text);
        }

        [Test]
        [Ignore("Country Border Config")] // TODO fix configuration
        public void ShouldSendLastUpdateWithCountryName()
        {
            locationService.Expect(x => x.GetLastLocationInfo(me))
                .Returns(new LocationInfo() { UserID = me, When = DateTime.Now, Location = new LatLong(38, -120) });

            MemoryBotClient bot = new MemoryBotClient("fnb");
            MemoryBotClient user = new MemoryBotClient("fnbtesta");
            bot.SetBuddy(user);
            user.SetBuddy(bot);

			PingProcessor processor = new PingProcessor(usersService.Object, locationService.Object, CreateMessageBuilder());
            Message message = CreateMessage("ping", "fnbtesta", "fnb");

            Assert.IsTrue(processor.CanProcess(message));
            var runNext = processor.Process(bot, message);
            Assert.AreEqual(RunNextProcessor.No, runNext);

            string location = "United States";
            string updated = "now";
            string text = string.Format("Service is running. Last location update was in {0} {1}. This message was sent at", location, updated);

            Assert.AreEqual(1, user.Messages.GetAll().Count());
            AssertString.StartsWith(text, user.Messages.GetAll().First().Text);
            AssertString.EndsWith("UTC.", user.Messages.GetAll().First().Text);
        }

        [Test]
        public void ShouldSendLastUpdate()
        {
            locationService.Expect(x => x.GetLastLocationInfo(me))
                .Returns(new LocationInfo() { UserID = me, When= DateTime.Now, Location = new LatLong(44, 33) });

            MemoryBotClient bot = new MemoryBotClient("fnb");
            MemoryBotClient user = new MemoryBotClient("fnbtesta");
            bot.SetBuddy(user);
            user.SetBuddy(bot);

			PingProcessor processor = new PingProcessor(usersService.Object, locationService.Object, CreateMessageBuilder());
            Message message = CreateMessage("ping", "fnbtesta", "fnb");

            Assert.IsTrue(processor.CanProcess(message));
            var runNext = processor.Process(bot, message);
            Assert.AreEqual(RunNextProcessor.No, runNext);

            string location = "44 33";
            string updated = "now";
            string text = string.Format("Service is running. Last location update was in {0} {1}. This message was sent at", location, updated);

            Assert.AreEqual(1, user.Messages.GetAll().Count());
            AssertString.StartsWith(text, user.Messages.GetAll().First().Text);
            AssertString.EndsWith("UTC.", user.Messages.GetAll().First().Text);            
        }
    }

}
